<template>
  <Dialog title="编辑并发模式" v-model="concurrDialog" width="500">
    <el-form ref="form" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入任务名称" />
      </el-form-item>
      <el-form-item label="并发模式">
        <el-select
          v-model="formData.concurrenceMode"
          placeholder="请选择并发模式"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in concurrenceModeOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="并发倍率(1-4)">
        <el-input-number v-model="formData.multiplier" :min="1" :max="4" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="concurrDialog = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup>
import * as taskApi from '@/api/call/task/index'
const concurrDialog = ref(false)
const formLoading = ref(false)
const formData = reactive({
  id: '',
  name: '',
  concurrenceMode: 0,
  multiplier: 1
})
const emit = defineEmits(['success'])

const concurrenceModeOptions = [
  {
    name: '智能',
    value: 0
  },
  {
    name: '固定',
    value: 1
  }
]
const formRules = reactive({
  name: [
    {
      required: true,
      message: '任务名称不能为空',
      trigger: 'blur'
    }
  ]
})
/*打开*/
const open = (item) => {
  concurrDialog.value = true
  const { id, name, concurrenceMode, multiplier } = item
  formData.id = id
  formData.name = name
  formData.concurrenceMode = concurrenceMode
  formData.multiplier = multiplier
}
defineExpose({ open })

/*提交*/
const submitForm = async () => {
  try {
    formLoading.value = true
    await taskApi.editTaskConCurr(formData)
    ElMessage.success('编辑成功!')
    concurrDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<style lang="scss" scoped></style>
